#include "mainwindow.h"

#include "ui_mainwindow.h"
#include <internetserver.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    on_off = false;
    mode = 1;
    s = new InternetServer;
    s->setMainWindow(this);
    startTimer(3000);
    // QLineSeries *series;
    // // QLineSeries *series2;
    // ui->widget_7->createcharviewFun(1,2);
    // ui->widget_7->createcharviewFun(2,6);
    // ui->widget_7->createcharviewFun(20,5);
    slotLoadSerialPort();
    slotOpenSerialPort();
}

MainWindow::~MainWindow()
{
    delete ui;
    delete s;
    delete l;
    delete q;
}

void MainWindow::timerEvent(QTimerEvent *event)
{
    if(on_off == true)
    {
        if(mode == 2)
        {
            c.C_send();
        }
    }
    // ui->widget_7->createcharviewFun(i,i);
    // i++;
}

void MainWindow::on_pushButton_clicked()
{
    connect(l, &login::sendParameter, this, &MainWindow::receiveParameter);
    l->show();
}

void MainWindow::receiveParameter(QString pixmap_inform)
{
    if(pixmap.load(pixmap_inform) == false)
    {
        qDebug()<<"加载"<<pixmap_inform<<"失败"<<Qt::endl;
    }
    ui->label->setPixmap(pixmap);
}


//2 0 状态
void MainWindow::on_checkBox_stateChanged(int arg1)
{
    if(arg1 == 2)
    {
        qDebug()<<"只有打开状态可以执行下面的代码";
        if(mode == 1){
            quint16 port = ui->lineEdit_2->text().toUShort();
            QHostAddress address;
            address = QHostAddress(ui->lineEdit->text());
            s->S_listen(address, port);
            s->S_connect();
        }
        else if(mode == 2)
        {
            const QString hostName = ui->lineEdit->text();
            quint16 port = ui->lineEdit_2->text().toUShort();
            c.C_connect(hostName, port);
            on_off = true;
        }
    }
}


void MainWindow::on_checkBox_2_stateChanged(int arg1)
{
    if(arg1 == 0)
    {
        ui->checkBox_2->setText("主机模式");
        mode = 1;
    }
    else if(arg1 == 2)
    {
        ui->checkBox_2->setText("客户机模式");
        mode = 2;
    }
}

void MainWindow::setArg(float temperature = 0, float MFS = 0, float dust_density = 0, float humidity = 0, QDateTime currentDateTime = QDateTime::currentDateTime())
{
    QString temperatureStr = QString::number(temperature);
    QString MFS_Str = QString::number(MFS);
    QString dust_densityStr = QString::number(dust_density);
    QString humidityStr = QString::number(humidity);
    ui->lineEdit_3->setText(temperatureStr);
    ui->lineEdit_4->setText(humidityStr);
    ui->lineEdit_5->setText(dust_densityStr);
    ui->lineEdit_6->setText(MFS_Str);
    qint64 x1 = currentDateTime.toMSecsSinceEpoch();
    // ui->widget_7->createcharviewFun(x1, temperature, "温度");
    // ui->widget_7->createcharviewFun(x1, MFS, "磁场");
    // ui->widget_7->createcharviewFun(x1, dust_density, "灰尘");
    // ui->widget_7->createcharviewFun(x1, humidity, "湿度");
}


void MainWindow::on_pushButton_2_clicked()
{
    q->show();
}


void MainWindow::on_lineEdit_3_textChanged(const QString &arg1)
{
    int value;
    value = ui->checkBox_3->checkState();
    if(value == 2)
    {
        float temperture = ui->lineEdit_7->text().toFloat();
        float current_temperture = arg1.toFloat();
        if(current_temperture > temperture)
        {
            qDebug()<<"温度超出阈值范围";
            ui->lineEdit_3->setStyleSheet("QLineEdit {background: red;}");
        }
        else
        {
            ui->lineEdit_3->setStyleSheet("QLineEdit {background: white;}");
        }
    }
}


void MainWindow::on_lineEdit_6_textChanged(const QString &arg1)
{
    int value;
    value = ui->checkBox_4->checkState();
    if(value == 2)
    {
        float temperture = ui->lineEdit_8->text().toFloat();
        float current_temperture = arg1.toFloat();
        if(current_temperture > temperture)
        {
            qDebug()<<"湿度超出阈值范围";
            ui->lineEdit_4->setStyleSheet(" QLineEdit {background: red;}");
        }
        else
        {
            ui->lineEdit_4->setStyleSheet("QLineEdit {background: white;}");
        }
    }
}


void MainWindow::on_lineEdit_5_textChanged(const QString &arg1)
{
    int value;
    value = ui->checkBox_5->checkState();
    if(value == 2)
    {
        float temperture = ui->lineEdit_9->text().toFloat();
        float current_temperture = arg1.toFloat();
        if(current_temperture > temperture)
        {
            qDebug()<<"灰尘超出阈值范围";
            ui->lineEdit_5->setStyleSheet(" QLineEdit {background: red;}");
        }
        else
        {
            ui->lineEdit_5->setStyleSheet("QLineEdit {background: white;}");
        }
    }
}


void MainWindow::on_lineEdit_4_textChanged(const QString &arg1)
{
    int value;
    value = ui->checkBox_6->checkState();
    if(value == 2)
    {
        float temperture = ui->lineEdit_10->text().toFloat();
        float current_temperture = arg1.toFloat();
        if(current_temperture > temperture)
        {
            qDebug()<<"磁场强度超出阈值范围";
            ui->lineEdit_6->setStyleSheet(" QLineEdit {background: red;}");
        }
        else
        {
            ui->lineEdit_6->setStyleSheet("QLineEdit {background: white;}");
        }
    }
}


void MainWindow::slotLoadSerialPort()
{
    QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) //获取有效的串口列表
    {
        m_serialPortName << info.portName();
        qWarning()<<"serialPortName:"<<info.portName();
    }
}


void MainWindow::slotCloseSerialPort()
{
    if (m_serialPort)
        m_serialPort->close();
}

void MainWindow::slotOpenSerialPort()
{
    m_serialPort = new QSerialPort(this);//新建一个窗口对象

    if(m_serialPort->isOpen())//判断对象是否已经打开，如果是就关掉
    {
        m_serialPort->clear();
        m_serialPort->close();
    }
    //设置串口名字
    m_serialPort->setPortName("COM6");
    //打开串口
    if (!m_serialPort->open(QIODevice::ReadWrite))//打开COM2串口，并且设置为读写状态
    {
        return;
    }

    m_serialPort->setBaudRate(115200);//波特率
    m_serialPort->setDataBits(QSerialPort::Data8);//数据位
    m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//数据流控制（暂时不管）

    m_serialPort->setParity(QSerialPort::NoParity);//奇偶校验位
    m_serialPort->setStopBits(QSerialPort::OneStop);//停止位


    //把串口的可读信号和槽函数连接
    connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(slotReadData()));
}

void MainWindow::slotReadData()
{
    QString data = m_serialPort->readAll();
    ui->lineEdit_3->setText(data);
    qWarning()<<"接收数据："<<data;
}

void MainWindow::slotSend()
{
    if (m_serialPort)
    {
        if (!m_serialPort->isOpen())
            qWarning()<<"请打开一个串口";

        // QString info = "hello";
        QByteArray sendBuf;
        //把字符串转为字节
        sendBuf.append("hello");// = info.toLocal8Bit();
        m_serialPort->write(sendBuf);
    }
}
